﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Microhard.Core;
using FluentValidation.Results;
using Microhard.Plat.CRM.Stores.Models;
using Microhard.Plat.CRM.Stores.Msg;
using Microhard.Plat.CRM.Stores.Interface;

namespace Microhard.Plat.CRM.Stores.Validators
{
    public class SubKAValidator : AbstractValidator<SubKAModel>
    {
        public SubKAValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.KAID).NotEmpty().WithMessage(SubKAMsg.NeedKA).When(r => r.SubID == Guid.Empty);
                RuleFor(u => u.Name).NotEmpty().WithMessage(KAMsg.NeedName).When(r => r.SubID == Guid.Empty);
                RuleFor(u => u.Name).Length(1, 50).WithMessage(KAMsg.NameMaxLength).When(r => r.SubID == Guid.Empty);
                RuleFor(u => u.Address).Length(0, 200).WithMessage(KAMsg.AddressMaxLength).When(r => r.SubID == Guid.Empty);
                RuleFor(u => u.Contractor).Length(0, 50).WithMessage(KAMsg.ContractorMaxLength).When(r => r.SubID == Guid.Empty);
                RuleFor(u => u.Cellphone).Length(0, 50).WithMessage(KAMsg.CellphoneMaxLength).When(r => r.SubID == Guid.Empty);
                RuleFor(u => u.CustomerCode).NotEmpty().WithMessage(SubKAMsg.NeedCustomeCode).When(r => r.SubID == Guid.Empty);
                RuleFor(u => u.CustomerCode).Length(1, 50).WithMessage(SubKAMsg.MaxCustomeCode).When(r => r.SubID == Guid.Empty);
                Custom(u =>
                {
                    ISubKA ka = IoC.Resolve<ISubKA>();
                    if (ka.IsNameDuplicate(u))
                    {
                        return new ValidationFailure("Name", KAMsg.DuplicateName);
                    }
                    if (ka.IsCustomeCodeDuplicate(u.CustomerCode))
                    {
                        return new ValidationFailure("CustomerCode", KAMsg.DuplicateName);
                    }
                    return null;
                });
            });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.Address).Length(0, 200).WithMessage(KAMsg.AddressMaxLength);
                RuleFor(u => u.Contractor).Length(0, 50).WithMessage(KAMsg.ContractorMaxLength);
                RuleFor(u => u.Cellphone).Length(0, 50).WithMessage(KAMsg.CellphoneMaxLength);
            });

            RuleSet("UpdateLocation", () =>
            {
                RuleFor(u => u.SubID).NotEmpty().WithMessage(SubKAMsg.NeedSubID);
                RuleFor(u => u.Baidulng).GreaterThan(0).WithMessage(SubKAMsg.NeedLng);
                RuleFor(u => u.Baidulat).GreaterThan(0).WithMessage(SubKAMsg.NeedLat);
            });
            RuleSet("Assign", () =>
            {
                RuleFor(u => u.SubID).NotEmpty().WithMessage(SubKAMsg.NeedSubID);
                Custom(u =>
                {
                    if (u.KAMonitorID == Guid.Empty)
                    {
                        return new ValidationFailure("NeedEmpl", SubKAMsg.NeedMonitor);
                    }
                    return null;
                });
            });

        }
    }

}
